<?php

namespace app\model;

use think\Model;

class MapMonster extends Model
{
    protected $name = 'midguaiwu';  //区域地图怪物详情

    protected $pk = 'id';

    protected $schema = [
        'id'          => 'int',

        'sid'         => 'int',
        'mid'         => 'int',
        'type'        => 'int', //1普通，2领主
        'havetime'    => 'int', //归属时间

        'gyid'        => 'int',
        'gname'       => 'string',
        'ginfo'       => 'string',
        'glv'         => 'int',
        'ghp'         => 'int',
        'gmaxhp'      => 'int',
        'ggj'         => 'int',
        'gfy'         => 'int',
        'gbj'         => 'int',
        'gxx'         => 'int',
        'gsd'         => 'int',

        'gexp'        => 'int',     //奖励修为

        'addtime'     => 'int',
    ];

    /**
     * 获取列表
     * @return array 列表
     */
    public function getList($where = array(), $order = 'id desc')
    {
        $data = $this->where($where)->order($order)->select();
        return $data;
    }

    public function getInfo($id)
    {
        $map = array();
        $map['id'] = $id;
        return $this->getWhereInfo($map);
    }

    /**
     * 获取信息
     * @param array $where 条件
     * @return array 信息
     */
    public function getWhereInfo($where)
    {
        $info = $this->where($where)->find();
        return $info;
    }

    public function getWhereValue($where, $value)
    {
        return $this->where($where)->value($value);
    }

    //数据新增
    public function add($data)
    {
        //启动事务
        $this->startTrans();
        try {
            $insertid = self::insertGetId($data);
            //提交事务
            $this->commit();
            return $insertid;
        } catch (\Exception $e) {
            //回滚事务
            $this->rollback();
        }
    }

    //数据更新
    public function saved($data): bool
    {
        $this->update($data);
        return true;
    }

    /**
     * 删除信息
     * @param int $id ID
     * @return bool 删除状态
     */
    public function del($id)
    {
        $map = array();
        $map['id'] = $id;
        return $this->where($map)->delete();
    }

    public function countNum($where): int
    {
        return $this->where($where)->count('id');
    }

}
